import java.awt.*;
import java.util.*;
import javax.swing.*;

import diplo.*;

/**
 * 
 */

/**
 * @author Matthew Crist
 *
 */
public class Main {
	// Light Blue: 58042731
	// Aqua: 8042731
	Random generator = new Random(42731);
	public Main() {
		int screen_height = Toolkit.getDefaultToolkit().getScreenSize().height;
		int screen_width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int total_rows = (int)(screen_height / 25);
		int total_cols = (int)(screen_width / 25);
		int total_cells = total_rows * total_cols;
		int row = 0;
		int col = 0;
		Canvas canvas = new Canvas();
		canvas.setSize(screen_width, screen_height);
		JFrame frame = new JFrame();
		frame.add(canvas);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(0, 0, screen_width, screen_height);
		frame.setVisible(true);
		DiploCell[][] diploMatrix = new DiploCell[total_rows+1][total_cols+1];

		// Time to draw the canvas:
		for(int i = 0; i < total_cells; i++) {
			int eco = Math.abs(generator.nextInt() % 255);
			int theo = Math.abs(generator.nextInt() % 255);
			int mil = Math.abs(generator.nextInt() % 255);
			int gov = Math.abs(generator.nextInt() % 255);
			
			DiploCell cell = new DiploCell(eco, mil, theo, gov);
			diploMatrix[row][col] = cell;
			
			cell.DrawCell(col*25, row*25, canvas.getGraphics());
			
			if((i % total_cols == 0) && i != 0) {
				// Go to next row
				row++;
				col = 0;
			} else {
				col++;
			}
		}
		
		int iterations = 0;
		
		while(true) {
			if(iterations > 20000) {
				break;
			} else {
				iterations++;
			}
			
			row = 0;
			col = 0;
			
			for(int i = 0; i < total_cells; i++) {
				ArrayList<Integer> ecoList = new ArrayList<Integer>();
				ArrayList<Integer> theoList = new ArrayList<Integer>();
				ArrayList<Integer> milList = new ArrayList<Integer>();
				ArrayList<Integer> govList = new ArrayList<Integer>();
			
				DiploCell cell;
			
				if(col > 0) {
					cell = diploMatrix[row][col-1];
					
					if(cell != null) {
						ecoList.add(new Integer(cell.GetEconomy()));
						theoList.add(new Integer(cell.GetTheology()));
						milList.add(new Integer(cell.GetMilitary()));
						govList.add(new Integer(cell.GetGovernment()));
					}
				
					if(row > 0) {
						cell = diploMatrix[row-1][col-1];
						
						if(cell != null) {							
							ecoList.add(new Integer(cell.GetEconomy()));
							theoList.add(new Integer(cell.GetTheology()));
							milList.add(new Integer(cell.GetMilitary()));
							govList.add(new Integer(cell.GetGovernment()));
						}
					
						cell = diploMatrix[row-1][col];
						
						if(cell != null) {			
							ecoList.add(new Integer(cell.GetEconomy()));
							theoList.add(new Integer(cell.GetTheology()));
							milList.add(new Integer(cell.GetMilitary()));
							govList.add(new Integer(cell.GetGovernment()));
						}
					}
				
					if(row < diploMatrix.length - 1) {
						cell = diploMatrix[row+1][col-1];
						
						if(cell != null) {						
							ecoList.add(new Integer(cell.GetEconomy()));
							theoList.add(new Integer(cell.GetTheology()));
							milList.add(new Integer(cell.GetMilitary()));
							govList.add(new Integer(cell.GetGovernment()));
						}
					
						cell = diploMatrix[row+1][col];
						
						if(cell != null) {							
							ecoList.add(new Integer(cell.GetEconomy()));
							theoList.add(new Integer(cell.GetTheology()));
							milList.add(new Integer(cell.GetMilitary()));
							govList.add(new Integer(cell.GetGovernment()));
						}
					}
				}
			
				if(col < diploMatrix[row].length - 1) {					
					cell = diploMatrix[row][col+1];
					
					if(cell != null) {						
						ecoList.add(new Integer(cell.GetEconomy()));
						theoList.add(new Integer(cell.GetTheology()));
						milList.add(new Integer(cell.GetMilitary()));
						govList.add(new Integer(cell.GetGovernment()));
					}
				
					if(row > 0) {
						cell = diploMatrix[row-1][col+1];
						
						if(cell != null) {							
							ecoList.add(new Integer(cell.GetEconomy()));
							theoList.add(new Integer(cell.GetTheology()));
							milList.add(new Integer(cell.GetMilitary()));
							govList.add(new Integer(cell.GetGovernment()));
						}
					}
				
					if(row < diploMatrix.length - 1) {
						cell = diploMatrix[row+1][col+1];
						
						if(cell != null) {							
							ecoList.add(new Integer(cell.GetEconomy()));
							theoList.add(new Integer(cell.GetTheology()));
							milList.add(new Integer(cell.GetMilitary()));
							govList.add(new Integer(cell.GetGovernment()));
						}
					}
				}
			
				cell = diploMatrix[row][col];
			
				this.Normalize(diploMatrix[row][col], ecoList, milList, theoList, govList);
				
				diploMatrix[row][col].DrawCell(col*25, row*25, canvas.getGraphics());
				
				if((i % total_cols == 0) && i != 0) {
					// Go to next row
					row++;
					col = 0;
				} else {
					col++;
				}
			}
		}
	}
	
	/*************************************************
	 * This is the algorithm that determines change...
	 */
	public void Normalize(DiploCell cell, ArrayList<Integer> ecoList, ArrayList<Integer> milList, ArrayList<Integer> theoList, ArrayList<Integer> govList) {
		// DO SOMETHING HERE!!!
		ecoList.add(new Integer(cell.GetEconomy()));
		milList.add(new Integer(cell.GetMilitary()));
		govList.add(new Integer(cell.GetGovernment()));
		theoList.add(new Integer(cell.GetTheology()));
		
		Collections.sort(ecoList);
		Collections.sort(milList);
		Collections.sort(govList);
		Collections.sort(theoList);
		
		int eco_median = ecoList.get(Math.abs(generator.nextInt()%ecoList.size()));
		int mil_median = milList.get(Math.abs(generator.nextInt()%milList.size()));
		int gov_median = govList.get(Math.abs(generator.nextInt()%govList.size()));
		int theo_median = theoList.get(Math.abs(generator.nextInt()%theoList.size()));
		
		cell.SetEconomy(eco_median);
		cell.SetMilitary(mil_median);
		cell.SetGovernment(gov_median);
		cell.SetTheology(theo_median);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Main();
	}
}